<?php

if (isset($_POST['action']) && $_POST['action'] == 'checkitem') {

	$bids = explode("\n", $_POST['items']);

	foreach($bids as $bid) {

		$player_bid = explode("RP bid by", $bid);

		$rp_amount = abs(trim($player_bid[0]));

		$name_item = explode(" for ", $player_bid[1]);

		$name = addslashes(trim($name_item[0]));

		$item_name = addslashes(trim(trim($name_item[1]), "[]"));

		$raid_id = $_POST['raid'];

		$rp_player = $player->getId($name);

		$rp_player = $rp_player[0];

		$player_id = $rp_player['player_id'];

		$players[$player_id]['bid'] = $bid;
		$players[$player_id]['rp'] = $rp_amount;


		
		
		if(empty($rp_player)) {
			$players[$player_id]['name'] = $name;
			$players[$player_id]['validated'] = 0;
			$players[$player_id]['bid'] = "Player data could not be found, please check the name";
		} else {
			$players[$player_id]['name'] = $rp_player['name'];
			$players[$player_id]['bid'] = addslashes($bid);
			$players[$player_id]['validated'] = 1;
		}

	}



	$selected_raid = $_POST['raid'];

	$smarty->assign('items', $_POST['items']);

	$smarty->assign('players', $players);

	$smarty->assign('selected_raid', $selected_raid);

	$smarty->display('raid_check_item_rp.tpl', $_SESSION['uid']);

} elseif (isset($_POST['action']) && $_POST['action'] == 'checkevent') {

	$event = explode("\n", $_POST['event']);

	$description = $event[0];

	$event_rp =  explode("RP for ", $description);

	$event_rp = abs($event_rp[0]);

	$players_rp = explode(", ", $event[1]);


	foreach($players_rp as $player_rp) {

		$name = addslashes(trim($player_rp));

		$raid_id = $_POST['raid'];

		$rp_player = $player->getId($name);

		$rp_player = $rp_player[0];

		$player_id = $rp_player['player_id'];

		$players[$player_id]['rp'] = $event_rp;

		if(empty($rp_player)) {
			$players[$player_id]['name'] = $name;
			$players[$player_id]['validated'] = 0;
			$players[$player_id]['bid'] = "Player data could not be found, please check the name";
		} else {
			$players[$player_id]['name'] = $rp_player['name'];
			$players[$player_id]['bid'] = addslashes($description);
			$players[$player_id]['validated'] = 1;
		}


	}


	$selected_raid = $_POST['raid'];

	$smarty->assign('selected_raid', $selected_raid);

	$smarty->assign('event', $_POST['event']);

	$smarty->assign('players', $players);

	$smarty->display('raid_check_event_rp.tpl', $_SESSION['uid']);

} elseif (isset($_POST['action']) && $_POST['action'] == 'updateitem') {

	$bids = explode("\n", $_POST['items']);

	$raid_id = $_POST['raid'];

	$rp_raid = $raid->get($raid_id);

	$rp_raid  = $rp_raid[0];

	foreach($bids as $bid) {

		$player_bid = explode("RP bid by", $bid);

		$rp_amount = abs(trim($player_bid[0]));

		$name_item = explode(" for ", $player_bid[1]);

		$name = addslashes(trim($name_item[0]));

		$item = addslashes(trim($name_item[1]));

		$raid_id = $_POST['raid'];

		$rp_player = $player->getId($name);

		$rp_player = $rp_player[0];

		$admin_user_id = $_SESSION['uid'];

		$status = $rp->save(0, $raid_id, $rp_player['user_id'], $rp_player['player_id'], $admin_user_id, 0, $rp_amount, $rp_raid['rp_category_id'], $bid, 1, 0);
	}

	$selected_raid = $_POST['raid'];

	$smarty->assign('selected_raid', $selected_raid);

	$previous_raids = $raid->get(0,1, 65);

	$smarty->assign('previous_raids', $previous_raids);

	$smarty->display('raid_update_item_rp.tpl', $_SESSION['uid']);

} elseif (isset($_POST['action']) && $_POST['action'] == 'updateevent') {

	$event = explode("\n", $_POST['event']);

	$description = addslashes($event[0]);

	$event_rp =  explode("RP for ", $description);

	$event_rp = abs($event_rp[0]);

	$players_rp = explode(", ", $event[1]);

	$raid_id = $_POST['raid'];

	$rp_raid = $raid->get($raid_id);

	$rp_raid  = $rp_raid[0];

	foreach($players_rp as $player_rp) {

		$name = trim($player_rp);

		$raid_id = $_POST['raid'];

		$rp_player = $player->getId($name);

		$rp_player = $rp_player[0];

		$admin_user_id = $_SESSION['uid'];

		$status = $rp->save(0, $raid_id, $rp_player['user_id'], $rp_player['player_id'], $admin_user_id, $event_rp, 0, $rp_raid['rp_category_id'], $description, 2, 0);
	}

	$previous_raids = $raid->get(0,1, 65);

	$selected_raid = $_POST['raid'];

	$smarty->assign('selected_raid', $selected_raid);

	$smarty->assign('previous_raids', $previous_raids);

	$smarty->display('raid_update_event_rp.tpl', $_SESSION['uid']);

} elseif (isset($_GET['action']) && $_GET['action'] == 'events') {

	$selected_raid = $_GET['raid'];

	$smarty->assign('selected_raid', $selected_raid);

	$previous_raids = $raid->get(0,1, 15);

	$smarty->assign('previous_raids', $previous_raids);

	$smarty->display('raid_update_event_rp.tpl', $_SESSION['uid']);

} elseif (isset($_GET['action']) && $_GET['action'] == 'items') {

	$selected_raid = $_GET['raid'];

	$smarty->assign('selected_raid', $selected_raid);

	$previous_raids = $raid->get(0,1, 65);

	$smarty->assign('previous_raids', $previous_raids);

	$smarty->display('raid_update_item_rp.tpl', $_SESSION['uid']);
}


?>